lib.use(['element', 'table', 'form','laydate','tablePlug'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var laydate=layui.laydate;
    var tablePlug = layui.tablePlug //表格插件
    var orderHeadTable;
    var searchFrm = layui.Form('searchFrm');

    table.set(common.layui.table.config(tablePlug));
    searchFrm.selectRender();

    laydate.render({elem: '#orderDate' ,range: '至'});
    laydate.render({elem: '#checkDate' ,range: '至'});

    // 渲染表格
    orderHeadTable = table.render({
        elem: '#orderHeadTable'
        , url: common.util.createUrl('/sale/orderHead/list')
        // 对分页请求的参数：page、limit重新设定名称
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        , cols: [[
            {type:'checkbox'}
            ,{type: 'numbers', title: '编号' }
            ,{ field: 'id',  hide: true }
            ,{ field: 'entId',  hide: true}
            ,{ field: 'id',  hide: true}
            ,{ title: '订单编码', field: 'orderNo' }
            ,{ title: '订单日期', field: 'orderDate' }
            ,{ field: 'storeId',  hide: true}
            ,{ title: '门店名称', field: 'storeName' }
            ,{ field: 'customerId',  hide: true}
            ,{ title: '客户编码', field: 'customerCode' }
            ,{ title: '客户名称', field: 'customerName' }
            ,{ title: '客户联系人', field: 'customerContactName' }
            ,{ title: '客户电话', field: 'customerContactTel' }
            ,{ title: '送货地址', field: 'customerContactAdd' }
            ,{ field: 'guideId',  hide: true}
            ,{ title: '导购员名称', field: 'guideName' }
            ,{ title: '订单状态', field: 'orderState' ,templet: function (row) { return common.dist.getValue("sale.OrderHead_OrderStateEnum",row.orderState);}}
            ,{ field: 'checkUserId',  hide: true}
            ,{ field: 'checkUserName',  hide: true}
            ,{ title: '审核时间', field: 'checkDate' }
            ,{ title: '订单类型', field: 'orderType' ,templet: function (row) { return common.dist.getValue("sale.OrderHead_OrderTypeEnum",row.orderType);}}
            ,{ title: '安装日期', field: 'installDate' }
            ,{ title: '安装标记', field: 'installFlag' ,templet: function (row) { return common.dist.getValue("sale.OrderHead_InstallFlagEnum",row.installFlag);}}
            ,{ title: '量尺标记', field: 'rulerFlag' ,templet: function (row) { return common.dist.getValue("sale.OrderHead_RulerFlagEnum",row.rulerFlag);}}
            ,{ field: 'remark',  hide: true}
            ,{ field: 'createUserId',  hide: true}
            ,{ field: 'createUserName',  hide: true}
            ,{ field: 'createTime',  hide: true}
            ,{ field: 'updateUserId',  hide: true}
            ,{ field: 'updateUserName',  hide: true }
            ,{ title: '修改时间', field: 'updateTime' }
        ]]
    });
    //监听行单击事件（单击事件为：rowDouble）
    table.on('rowDouble(orderHeadTableFilter)', function(obj){
        update(obj)
    });
    $(document).on('click','#del',function(){
        del(table.checkStatus('orderHeadTable'))
    });
    $(document).on('click','#check',function(){
        check(table.checkStatus('orderHeadTable'))
    });
    $(document).on('click','#add',function(){
        var config = {
            title:"新增销售订单",
            content: 'sale/orderHead/orderHeadForm.html'
        }
        common.util.openWindow(busCommon.windowConfig(config))
    });
    $(document).on('click','#export',function(){
        var params=common.from.serializeObject('#searchFrm');
        params['pages']=orderHeadTable.config.page.pages;
        params['limit']=orderHeadTable.config.page.limit;
        common.util.exportWindows('/sale/orderHead/export',params)
    });
    form.on('submit(searchFilter)', function(data){
        var param = data.field;
        searchTable(param)
        return false;
    });


    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
       params=common.from.serializeObject('#searchFrm');
       orderHeadTable=table.reload('orderHeadTable',{
                where:params,
                page: {curr: 1 }
            }
        )
    }
    /**
     * 修改
     * @param obj
     */
    function update(obj) {
        if(!common.auth.hasPermission("sale:orderHead:update")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }
        if(postData.length!=1)return;
        var config = {
            title:"修改销售订单",
            content: 'sale/orderHead/orderHeadForm.html?id=' + postData[0]
        }
        common.util.openWindow(busCommon.windowConfig(config))
    }
    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        if(!common.auth.hasPermission("sale:orderHead:delete")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item;
            });
        }else{
            postData=[obj.data]
        }

        if(postData.length!=1){common.util.error('请选择一条记录进行操作');return;}
        if(postData[0].orderState!=1){common.util.error('[{0}] 当前单据非未审核状态,,禁止删除'.format(postData[0].probNo));return;}
        common.util.confirm('确认删除!', function(index){
            console.log('Config Del param is', postData);
            common.ajax.get('/sale/orderHead/delete/'+postData[0].id,null, function (resp) {
                searchTable();
                layer.close(index);
            })

        });
    }

    /**
     * 审核
     * @param obj
     */
    function check(obj) {
        if(!common.auth.hasPermission("sale:orderHead:check")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item;
            });
        }else{
            postData=[obj.data]
        }

        if(postData.length!=1){common.util.error('请选择一条记录进行操作');return;}
        common.util.confirm('确认审核!', function(index){
            console.log('Config Check param is', postData);
            common.ajax.get('/sale/orderHead/check/'+postData[0].id, null, function (resp) {
                searchTable();
                layer.close(index);
                common.util.success("审核成功")
            })

        });
    }
    window.formCallBack=function (params) {
        searchTable(params);
    };

});
